import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import CategoryView from "../views/CategoryView.vue";
import ProductView from "../views/ProductView.vue";
import PromoView from "../views/PromoView.vue";
import ShowcaseView from "../views/ShowcaseView.vue";
// import UserView from "../views/UserView.vue";
import AuthView from "../views/AuthView.vue";
import { useUserStore } from "../stores";
const router = createRouter({
  history: createWebHistory(import.meta.env.SERVER_URL),
  routes: [
    {
      path: "/",
      name: "Home",
      component: HomeView,
      beforeEnter: async () => {
        const userStore = useUserStore();
        await userStore.loginFromLocalStorage();
        if (!userStore.token) return { name: "Auth" };
      },
      redirect: "/products",
      children: [
        {
          path: "/categories",
          name: "CATEGORY",
          component: CategoryView,
        },
        {
          path: "/promos",
          name: "PROMOS",
          component: PromoView,
        },
        {
          path: "/showcases",
          name: "SHOWCASES",
          component: ShowcaseView,
        },
        {
          path: "/products",
          name: "PRODUCTS",
          component: ProductView,
        },
      ],
    },
    {
      path: "/auth",
      name: "Auth",
      component: AuthView,
    },
  ],
});

export default router;
